var addcion = '<div class="layui-form-item"><div class="layui-inline"><label class="layui-form-label">货币类型</label><div class="layui-input-inline" style="width: 200px;"><select lay-ignore class="layui-input coin_id coin-type" lay-filter="aihao"><option value=""></option></select></div><div class="layui-form-mid">比例</div><div class="layui-input-inline" style="width: 200px;"><input type="text" autocomplete="off" class="layui-input config"><span class="layui-input-bq">%</span></div><div class="layui-input-inline" style="width: 38px;"><a class="layui-btn layui-bnt-sc layui-btn-tr" onclick="delCion($(this))"  href="javascript:;"><i class="layui-icon">&#xe640;</i></a></div></div></div>'
//添加货币配置
function addCion() {
	$("#coinForm").append(addcion);
	$("#coinForm .layui-form-item:last-child").find("select.coin-type").html($("#cionHtml").html());
}

//删除货币配置
function delCion($this) {
	$this.parents(".layui-form-item").remove();
}

//根据货币ID匹配货币名称
function cinName(id) {
	var coinName;
	$.each($.parseJSON(coin), function(index, value) {
		if(value.id == id) {
			coinName = value.coin_name
		}
	});
	return coinName
}

//获取当前货币配置
function GainCoin() {
	$.ajax({
		type: 'post',
		dataType: 'json',
		url: coinpzSrc,
		data: 'bonus=xiaoshou&config=coin',
		success: function(data) {
			var data = data.config_value;
			$.each(data, function(index, value) {
				$("#coinForm").append('<div class="layui-form-item"><div class="layui-inline"><label class="layui-form-label">货币类型</label><div class="layui-input-inline" style="width: 200px;"><select lay-ignore class="layui-input coin-type coin_id" lay-filter="aihao"><option value="' + value.coin_id + '">' + cinName(value.coin_id) + '</option></select></div><div class="layui-form-mid">比例</div><div class="layui-input-inline" style="width: 200px;"><input type="text" value="' + value.config + '" autocomplete="off" class="layui-input config"><span class="layui-input-bq">%</span></div><div class="layui-input-inline" style="width: 38px;"><a class="layui-btn layui-bnt-sc layui-btn-tr" onclick="delCion($(this))"  href="javascript:;"><i class="layui-icon">&#xe640;</i></a></div></div></div>');
			})

		}
	});
}

//根据等级ID匹配等级名称
function levelName(id) {
	var levelName;
	$.each($.parseJSON(level), function(index, value) {
		if(value.id == id) {
			levelName = value.level_name
		}
	});
	return levelName
}

//添加倍数配置
function addple($this) {
	$this.parents(".layui-level-list").find("form#inviterForm").append('<div class="layui-form-item"><label class="layui-form-label">倍数</label><div class="layui-input-inline" style="width: 200px;"><input type="text" value="0" autocomplete="off" class="layui-input multiple"></div><div class="layui-form-mid">比列</div><div class="layui-input-inline" style="width: 200px;"><input type="text" value="0" autocomplete="off" class="layui-input rate"><span class="layui-input-bq">%</span></div><div class="layui-input-inline" style="width: 38px;"><a class="layui-btn layui-bnt-sc layui-btn-tr" onclick="delCion($(this))"  href="javascript:;"><i class="layui-icon">&#xe640;</i></a></div></div>')
}

//删除倍数配置
function delLevel($this) {
	$this.parents(".layui-level-list").remove();
}

//获取当前销售奖金配置
function GainLevel() {
	$.ajax({
		type: 'post',
		dataType: 'json',
		url: coinpzSrc,
		data: 'bonus=xiaoshou&config=bonus',
		success: function(data) {
			var data = data.config_value;
			$.each(data, function(i,v) {
				$("#level-list").append('<div class="layui-level-list" data-id="'+v.level_id+'"><blockquote class="layui-elem-quote" style="padding: 10px 15px; border-left: none;">' + levelName(v.level_id) + '<a href="javascript:void(0);" onclick="addple($(this))" class="layui-btn layui-btn-sm layui-btn-normal layui-btn-ff" style="margin-left: 20px;"><i class="layui-icon">&#xe654;</i>增加倍数配置</a></blockquote><div id="inviterConfig"><form id="inviterForm"></form></div></div>');
				$.each(v.ple_data,function(j,k){
					$("#level-list .layui-level-list").find("form#inviterForm").eq(i).append('<div class="layui-form-item"><label class="layui-form-label">倍数</label><div class="layui-input-inline" style="width: 200px;"><input type="text" value="'+k.multiple+'" autocomplete="off" class="layui-input multiple"></div><div class="layui-form-mid">比列</div><div class="layui-input-inline" style="width: 200px;"><input type="text" value="'+k.rate+'" autocomplete="off" class="layui-input rate"><span class="layui-input-bq">%</span></div><div class="layui-input-inline" style="width: 38px;"><a class="layui-btn layui-bnt-sc layui-btn-tr" onclick="delCion($(this))"  href="javascript:;"><i class="layui-icon">&#xe640;</i></a></div></div></div>');
				})
			})

		}
	});
}


//获取当前销售奖金封顶值
function GainCapping() {
	$.ajax({
		type: 'post',
		dataType: 'json',
		url: coinpzSrc,
		data: 'bonus=xiaoshou&config=max',
		success: function(data) {
			var data = data.config_value;
			$.each(data, function(index,value) {
				$("#CappingForm").append('<div class="layui-form-item" data-id="'+value.level_id+'"><div class="layui-inline"><label class="layui-form-label">' + levelName(value.level_id) + '</label><div class="layui-input-inline" style="width: 460px;"><input type="text" value="'+value.max+'" autocomplete="off" class="layui-input max"></div></div></div>');
				
			})

		}
	});
}

layui.use('layer', function() {
	var $ = layui.jquery,
		layer = layui.layer;
	var active = {
		//保存货币配置
		SubmitCoin: function() {
			var Array = [];
			$("#coinForm .layui-form-item").each(function(index) {
				var coin_id = $(this).find("select.coin_id option:selected").val();
				var config = $(this).find("input.config").val();
				var data = {
					coin_id: coin_id,
					config: config
				};
				Array.push(data);
			});
			console.log(Array)
			$.ajax({
				type: 'post',
				dataType: 'json',
				url: SubmitCoinSrc,
				data: {
					token: token,
					bonus_name: "xiaoshou",
					config_name: "coin",
					data: Array
				},
				success: function(data) {
					var code = data.code;
					var msg = data.msg;
					if(code == 1) {
						layer.alert(msg, {
							icon: 1
						}, function() {
							location.reload();
						})
					} else {
						layer.alert(msg, {
							icon: 5
						})
					}
				}
			});
		},
		//保存等级倍数配置
		SubmitLevel: function() {
			var arr =new Array();
			$(".layui-field-box .layui-level-list").each(function(index) {
				var level_id = $(this).attr("data-id");
				var item = $(this);
				var pelArr=new Array();
				item.find("div.layui-form-item").each(function(index) {
					var multipe=$(this).find("input.multiple").val();
					var rate=$(this).find("input.rate").val();
					var peldata={
						multiple:multipe,
                        rate:rate
					}
					pelArr.push(peldata);
				})
				var data = {
						level_id: level_id,
						ple_data:pelArr
				};
				arr.push(data);
			});
			$.ajax({
				type: 'post',
				dataType: 'json',
				url: SubmitCoinSrc,
				data: {
					token: token,
					bonus_name: "xiaoshou",
					config_name: "bonus",
					data: arr
				},
				'success': function(data) {
					var code = data.code;
					var msg = data.msg;
					if(code == 1) {
						layer.alert(msg, {
							icon: 1
						}, function() {
							location.reload();
						})
					} else {
						layer.alert(msg, {
							icon: 5
						})
					}
				}
			});

		},
		
		//保存销售奖金封顶值配置
		SubmitCapping: function(){
			var arr =new Array();
			$("#CappingForm .layui-form-item").each(function(index) {
				var level_id = $(this).attr("data-id");
				var max=$(this).find("input.max").val();
				var data = {
						level_id: level_id,
						max:max
				};
				arr.push(data);
			});
			$.ajax({
				type: 'post',
				dataType: 'json',
				url: SubmitCoinSrc,
				data: {
					token: token,
					bonus_name: "xiaoshou",
					config_name: "max",
					data: arr
				},
				'success': function(data) {
					var code = data.code;
					var msg = data.msg;
					if(code == 1) {
						layer.alert(msg, {
							icon: 1
						}, function() {
							location.reload();
						})
					} else {
						layer.alert(msg, {
							icon: 5
						})
					}
				}
			});
			
			
		}
	};
	$('.right-center').on('click', '.right-btn', function() {
		var othis = $(this),
			method = othis.data('method');
		active[method] ? active[method].call(this, othis) : '';
	});

})

$(function() {
	cionFun();
	GainCoin();
	GainLevel();
	GainCapping()
})